// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

/*!
\file 
\brief Header File for the PMODEL (BT) Association Function
*/
#ifndef _PMODEL_ASSOC_H_
#define _PMODEL_ASSOC_H_

#include <stdio.h>

#include "adjusted.h"
#include "bitstring.h"
#include "bmapfile.h"
#include "sample.h"
#include "types.h"

#ifdef __cplusplus
extern "C" {
#endif

void pmodel_write_file_header(FILE *out, struct selected_options *ops);

/*!
\ingroup pmodel 
\brief Calculation code of PMODEL (BT).
\details
<p>Code based on the PLINK <em>model</em> %option.</p>
<p>This function runs in 2 distinct modes.<br>
It can run in a simple calculation mode or in a result persistence mode
where data is written to file.<br>
To switch on the persistence mode, the FILE output handle and the adjusted parameters
must not be NULL.</p>

<p>When in calculation mode, the FILE output handle, adjusted struct, EMP1/EMP2 parameters can be set to NULL 
or zero.</p>

<p>%Model calculations performed are specified by setting the appropriate flag bound 
to the options structure (\ref selected_options) to TRUE.</p>

<p>Following the logic of PLINK, if only a single model %option is specified then this functions returns a CHISQ value for that calculation.<br>
If more than 1 model %option is specified, this function returns 1 - Best Pvalue for all the calculations run.
</p>

\param [in] X Flag that SNP on sex chromsome
\param [in] haploid Flag that SNP is haploid
\param [in] ones_and_twos BitString containing the allelic data of a SNP
\param [in] samples Data-set %sample array (people and phenotypes)
\param [in] nSamples Size of the %sample array
\param [in] ops Models to run.
\param [in] snp SNP under investigation
\param [in,out] out Output file handle
\param [in] emp1 Empirical P value ratio for a SNP
\param [in] emp2 %Family Wise Error Rate (FWER) for a SNP
\param [in] adj Structure containing the adjusted p-values (BONF, HOLM....)
\param [in] zt Normalised confidence interval threshold (zt = ltqnorm(1 - (1 - ops.ci_level) / 2))
\returns double or JUNK_CHISQ is calculation went mental.
\sa Plink::fullModelAssoc()
\sa selected_options::model_perm_gen 
\sa selected_options::model_perm_dom 
\sa selected_options::model_perm_rec 
\sa selected_options::model_perm_trend 
\sa selected_options::model_perm_allelic
*/
double pmodel_assoc(BOOL X, BOOL haploid, BitString ones_and_twos, struct sample *samples, int nSamples, struct selected_options *ops, struct small_locus *snp, FILE *out, double emp1, double emp2, struct adjusted *adj, double zt);

/*!
\brief Write the selected model options to file
\param ops Model options and output file location
\returns 1 on success, 0 on failure
*/
int pmodel_write_model_options(struct selected_options *ops);

/*!
\brief Read the selected model options from file
\param ops Input file location
\returns 1 on success, 0 on failure
*/
int pmodel_read_model_options(struct selected_options *ops);

#ifdef __cplusplus
}
#endif

#endif // _PMODEL_ASSOC_H_